from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class College(models.Model):
    """学院表"""
    college_name = models.CharField(max_length=255, verbose_name='学院名', db_comment='学院名称')
    
    class Meta:
        db_table = 'zongce_college'
        verbose_name = '学院'
        verbose_name_plural = '学院'
        db_table_comment = '学院表，存储学院基本信息'
    
    def __str__(self):
        return str(self.college_name)


class Major(models.Model):
    """专业表"""
    major_name = models.CharField(max_length=25, verbose_name='专业名', db_comment='专业名称')
    college = models.ForeignKey(
        College,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='majors',
        verbose_name='所属学院',
        db_comment='所属学院ID'
    )
    
    class Meta:
        db_table = 'zongce_major'
        verbose_name = '专业'
        verbose_name_plural = '专业'
        db_table_comment = '专业表，存储专业信息及所属学院'
    
    def __str__(self):
        return str(self.major_name)


class Classes(models.Model):
    """班级表"""
    class_name = models.CharField(max_length=255, unique=True, verbose_name='班级', db_comment='班级名称')
    teacher = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='managed_classes',
        verbose_name='管理教师',
        db_comment='管理教师ID'
    )
    major = models.ForeignKey(
        Major,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='classes',
        verbose_name='所属专业',
        db_comment='所属专业ID'
    )
    
    class Meta:
        db_table = 'zongce_classes'
        verbose_name = '班级'
        verbose_name_plural = '班级'
        db_table_comment = '班级表，存储班级信息、管理教师和所属专业'
    
    def __str__(self):
        return str(self.class_name)


class Course(models.Model):
    """课程表"""
    course_name = models.CharField(max_length=255, verbose_name='课程名', db_comment='课程名称')
    major = models.ForeignKey(
        Major,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='courses',
        verbose_name='所属专业',
        db_comment='所属专业ID'
    )
    teacher_name = models.CharField(max_length=100, blank=True, verbose_name='任课教师', db_comment='任课教师姓名')
    
    class Meta:
        db_table = 'zongce_course'
        verbose_name = '课程'
        verbose_name_plural = '课程'
        db_table_comment = '课程表，存储课程基本信息、所属专业及任课教师'
    
    def __str__(self):
        return str(self.course_name)


class Punish(models.Model):
    """处分表"""
    sort = models.CharField(max_length=50, blank=True, verbose_name='类别', db_comment='处分类别')
    name = models.CharField(max_length=50, blank=True, verbose_name='处分名称', db_comment='处分名称')
    ranks = models.CharField(max_length=50, blank=True, verbose_name='级别', db_comment='处分级别')
    grade = models.IntegerField(null=True, blank=True, verbose_name='扣分数', db_comment='扣除分数')
    teacher = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='punishments_given',
        verbose_name='辅导员',
        db_comment='辅导员ID'
    )
    student = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='punishments_received',
        verbose_name='受处分学生',
        db_comment='受处分学生ID'
    )
    time = models.IntegerField(null=True, blank=True, verbose_name='年份', db_comment='年份')
    
    created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name='创建时间', db_comment='创建时间')
    
    class Meta:
        db_table = 'zongce_punish'
        verbose_name = '处分'
        verbose_name_plural = '处分'
        ordering = ['-created_at']
        db_table_comment = '处分表，记录学生违纪处分信息'
    
    def __str__(self):
        return f"{self.name} - {self.student.username}"


class PunishDict(models.Model):
    """处分字典表"""
    sort = models.CharField(max_length=50, blank=True, verbose_name='类别', db_comment='处分类别')
    name = models.CharField(max_length=50, blank=True, verbose_name='处分名称', db_comment='处分名称')
    ranks = models.CharField(max_length=50, blank=True, verbose_name='级别', db_comment='处分级别')
    grade = models.IntegerField(default=0, verbose_name='扣分数', db_comment='对应扣分')
    
    class Meta:
        db_table = 'zongce_punish_dict'
        verbose_name = '处分字典'
        verbose_name_plural = '处分字典'
        ordering = ['-id']
        db_table_comment = '处分字典表，存储各类处分的基本信息和扣分标准'
    
    def __str__(self):
        return f"{self.sort} - {self.name} - {self.ranks}"


class Score(models.Model):
    """综测分表"""
    student = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='comprehensive_scores',
        verbose_name='学生',
        db_comment='学生ID'
    )
    time = models.IntegerField(verbose_name='年份', db_comment='学年')
    grade_level = models.CharField(
        max_length=10,
        blank=True,
        choices=[('大一', '大一'), ('大二', '大二'), ('大三', '大三'), ('大四', '大四')],
        verbose_name='年级',
        db_comment='学生年级（大一/大二/大三/大四）'
    )
    grade = models.DecimalField(max_digits=6, decimal_places=3, null=True, blank=True, verbose_name='综测分', db_comment='综合测评总分')
    
    # 基本分
    zb_grade = models.DecimalField(max_digits=6, decimal_places=3, null=True, blank=True, verbose_name='基本智育分', db_comment='基本智育分')
    db_grade = models.DecimalField(max_digits=6, decimal_places=3, null=True, blank=True, verbose_name='基本德育分', db_comment='基本德育分')
    tb_grade = models.DecimalField(max_digits=6, decimal_places=3, null=True, blank=True, verbose_name='基本体育分', db_comment='基本体育分')
    
    # 附加分
    za_grade = models.DecimalField(max_digits=6, decimal_places=3, null=True, blank=True, verbose_name='附加智育分', db_comment='附加智育分')
    da_grade = models.DecimalField(max_digits=6, decimal_places=3, null=True, blank=True, verbose_name='附加德育分', db_comment='附加德育分')
    ta_grade = models.DecimalField(max_digits=6, decimal_places=3, null=True, blank=True, verbose_name='附加体育分', db_comment='附加体育分')
    
    fail = models.IntegerField(null=True, blank=True, verbose_name='挂科数', db_comment='挂科门数')
    
    created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name='创建时间', db_comment='创建时间')
    updated_at = models.DateTimeField(auto_now=True, null=True, blank=True, verbose_name='更新时间', db_comment='更新时间')
    
    class Meta:
        db_table = 'zongce_score'
        verbose_name = '综测分'
        verbose_name_plural = '综测分'
        unique_together = [['student', 'time']]
        ordering = ['-time', 'student']
        db_table_comment = '综测分表，存储学生综合测评成绩，包括基本分和附加分'
    
    def __str__(self):
        return f"{self.student.username} - {self.time}年 - {self.grade}"
